package ncme.auto.framework.core.entities;

import java.util.HashMap;
import java.util.Map;

public class ResMsg {

    private String state;
    private int code;
    private String msg;
    private Map<String, Object> data;

    public ResMsg(String state, int code, String msg, Map<String, Object> data) {
        this.state = state;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ResMsg successRes(Object result) {
        Map<String, Object> map = new HashMap<>();
        map.put("result", result);
        return succeseeRes("请求成功", result != null ? map : null);
    }

    public static ResMsg successRes(Map<String, Object> data) {
        return succeseeRes("请求成功", data);
    }

    public static ResMsg successResOnlyToken(String token) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        return succeseeRes("请求成功", map);
    }

    public static ResMsg succeseeRes(String msg, Map<String, Object> data) {
        return new ResMsg("成功", 1001, msg, data);
    }

    //code为-1默认1101
    public static ResMsg succeseeResOneMes(String msg, Integer code) {
        if (code == -1)
            code = 1001;
        return new ResMsg("成功", code, msg, null);
    }

    public static ResMsg failRes(Map<String, Object> data) {
        return failRes("请求失败", data);
    }

    //code为-1默认1101
    public static ResMsg failResOneMes(String msg, Integer code) {
        if (code == -1)
            code = 1101;
        return new ResMsg("失败", code, msg, null);
    }

    public static ResMsg failRes(String msg, Map<String, Object> data) {
        return new ResMsg("失败", 1101, msg, data);
    }


    @Override
    public String toString() {
        return "ResMsg{" + "state='" + state + '\'' + ", code=" + code + ", msg='" + msg + '\'' + ", data=" + data + '}';
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    public void addData(String key, Object value) {
        if (this.data == null) this.data = new HashMap<>();
        this.data.put(key, value);
    }

}
